﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Microsoft.WindowsMobile;
using Microsoft.WindowsMobile.Status;
using Microsoft.WindowsMobile.Telephony;

using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;

using System.Runtime.InteropServices;
using Microsoft.Win32;

using System.Diagnostics;

namespace Olympus_Q8_m6
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private string smsFormat = "olympus_1_{0}";
        //Send to Desination call for notice
        private string monitorStr = "olympus_2_";
        //Notice souce call for sms
        private void button1_Click(object sender, EventArgs e)
        {
            Button bt = (Button)sender;
            tbPhone.Text += bt.Text;
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            if (startCall)
            {
                startCall = false;
                plKey.Visible = true;
                plStatus.Visible = false;
            }
            else
                this.Close();
        }

        private void button13_Click(object sender, EventArgs e)
        {
            if (tbPhone.Text.Length > 0)
                tbPhone.Text = tbPhone.Text.Substring(0, tbPhone.Text.Length -1);
        }
        private bool startCall = false;
        private MessageInterceptor messageInteceptor = new MessageInterceptor();
        private void button14_Click(object sender, EventArgs e)
        {
            startCall = true;
            Rimage = false;
            Rsound = false;
            plKey.Visible = false;
            plStatus.Visible = true;
            this.Refresh();
            tbStatus.Text = string.Format("开始通知 {0}\r\n", tbPhone.Text);
            PlaySound("\\1.wav", IntPtr.Zero, SND_ASYNC );

            if (!SmsSend(tbPhone.Text))
            {
                MessageBox.Show("发送通知消息失败.");
                plKey.Visible = true;
                plStatus.Visible = false;
                return;
            }
            tbStatus.Text += "发送通知消息成功, 等待回应......\r\n";
            PlaySound("\\1.wav", IntPtr.Zero, SND_ASYNC );

            //Microsoft.WindowsMobile.Telephony.Phone phone = new Phone();
            //phone.Talk(tbPhone.Text, false);
        }
        private bool Rimage = false, Rsound = false;
        private bool SmsSend(string phoneNumber)
        {
            try
            {
                SmsMessage sms = new SmsMessage();
                sms.Body = string.Format(smsFormat,phoneNumber);
                sms.To.Add(new Recipient(phoneNumber));
                sms.Send();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        void messageInteceptor_MessageReceived(object sender, MessageInterceptorEventArgs e)
        {
            if (!startCall)
                return;
            if (e.Message is SmsMessage)
            {
                SmsMessage sms = (SmsMessage)e.Message;
#if DEBUG
                Debug.WriteLine(sms.Body);
                Debug.WriteLine(startCall.ToString());

#endif
                
                if (sms.Body.EndsWith("_0"))
                {
                    Rimage = true;
                    this.tbStatus.Text += "图片下载成功.\r\n";
                    PlaySound("\\1.wav", IntPtr.Zero, SND_ASYNC );
                    this.Refresh();
                }
                if (sms.Body.EndsWith("_1"))
                {
                    Rsound = true;
                    this.tbStatus.Text += "铃声下载成功.\r\n";
                    this.Update();
                    PlaySound("\\1.wav", IntPtr.Zero, SND_ASYNC );
                }
            }
            if (Rimage && Rsound)
            {
                //messageInteceptor.MessageReceived -= new MessageInterceptorEventHandler(messageInteceptor_MessageReceived);
                Rimage = false;
                Rsound = false;
                startCall = false;
                Microsoft.WindowsMobile.Telephony.Phone phone = new Phone();
                phone.Talk(tbPhone.Text, false);
                this.Close();
            }
        }
        //private string localNumber = "";
        private void MainForm_Load(object sender, EventArgs e)
        {
            //this.Menu = mainMenu2;
            //lbTemp.Visible = false;

            //if (!System.IO.File.Exists("\\My Documents\\phone.ini"))
            //{
            //    this.Menu = mainMenu2;
            //    btStart.Visible = false;
            //    lbTemp.Visible = true;
            //}
            //else
            //{
            //    lbTemp.Visible = false;
            //    System.IO.StreamReader sr = new System.IO.StreamReader("\\My Documents\\phone.ini");
            //    lbLocalPhone.Text = sr.ReadLine();
            //    sr.Close();
            //}
            //localNumber = Marshal.PtrToStringUni(GetPhoneNumber());
            //if (localNumber.Length != 11 || !localNumber.StartsWith("1"))
            //{
            //    this.Menu = mainMenu2;
            //    btStart.Visible = false;
            //    lbTemp.Visible = true;
            //}
            MessageCondition mc = new MessageCondition();
            messageInteceptor.InterceptionAction = InterceptionAction.NotifyAndDelete;

            mc.ComparisonType = MessagePropertyComparisonType.StartsWith;
            mc.Property = MessageProperty.Body;
            mc.CaseSensitive = false;
            mc.ComparisonValue = monitorStr;
            messageInteceptor.MessageCondition = mc;
            messageInteceptor.MessageReceived += new MessageInterceptorEventHandler(messageInteceptor_MessageReceived);
        }
        //[DllImport("PhoneNumber.dll", CharSet = CharSet.Unicode)]
        //public static extern IntPtr GetPhoneNumber();

        private void menuItem4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            System.IO.StreamWriter sw = new System.IO.StreamWriter("\\My Documents\\Phone.ini");
            sw.WriteLine(tbPhone.Text);
            sw.Close();
            tbPhone.Text = "";
            lbTemp.Visible = false;
            btStart.Visible = true;
            this.Menu = mainMenu1; ;
        }

        [System.Runtime.InteropServices.DllImport("coredll.dll")]
        static extern int PlaySound(string pszSound, IntPtr hMod, int fdwSound);

        const int SND_FILENAME = 0x20000;
        const int SND_SYNC = 0x0;
        const int SND_ASYNC = 0x1;
        const int SND_LOOP = 0x8;
        const int SND_PURGE = 0x40;
        const int SND_MEMORY = 0x04;
        const int SND_NODEFAULT = 0x02;

    }
}